package com.wlcorp.forcazamis.sys.util;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtendVoid {

    public static String replace(String source, String oldString,
            String newString) {
        if (source == null)
            return null;
        StringBuffer output = new StringBuffer();
        int lengOfsource = source.length();
        int lengOfold = oldString.length();
        int posStart = 0;
        int pos;
        while ((pos = source.indexOf(oldString, posStart)) >= 0) {
            output.append(source.substring(posStart, pos));
            output.append(newString);
            posStart = pos + lengOfold;
        }
        if (posStart < lengOfsource) {
            output.append(source.substring(posStart));
        }
        return output.toString();
    }

    public static String[] spiltStr(String fieldsru, String tag) {
        char dot = tag.charAt(0);
        String field;
        field = fieldsru + dot;
        int num = 0;
        int field_len = field.length();
        for (int i = 0; i < field_len; i++) {
            if (field.charAt(i) == dot) {
                num++;
            }
        }
        String[] returnarray = new String[num];
        int begin = 0;
        int end;
        for (int j = 0; j < num; j++) {
            end = field.indexOf(dot, begin);
            returnarray[j] = field.substring(begin, end);
            begin = end + 1;
        }
        return returnarray;
    }

    public static int spiltStrNum(String fieldsru, String tag) {
        char dot = tag.charAt(0);
        String field;
        field = fieldsru + dot;
        int num = 0;
        int field_len = field.length();
        for (int i = 0; i < field_len; i++) {
            if (field.charAt(i) == dot) {
                num++;
            }
        }
        return num;
    }

    public static int getStringLength(String s){
        int lengthNo = 0;
        if (null != s){
            for (int i = 1; i <= s.length(); i++){
                String ss = s.substring(i-1,i);
                if (ss.charAt(0) > 255){
                    lengthNo = lengthNo + 2;
                }else{
                    lengthNo ++;
                }
            }
        }
        return lengthNo;
    }

    public static int toIntValid(String s) {
        try{
            return Integer.parseInt(s);
        }catch(Exception e){
            return 0;
        }
    }

    public static float toFloatValid(Float s) {
        float floatValue = 0;
        if (null != s){
            floatValue = s.floatValue();
        }
        return floatValue;
    }

    /**
     * 是否是整型或浮点�?
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        if(null == str||str.equals("")||str.equals("null")){
            return false;
        }
        if(isInt(str))
            return true;
        Pattern pattern = Pattern.compile("^[0-9]{1,}\\.[0-9]{1,}$");
        Matcher isNum = pattern.matcher(str);
        if(!isNum.matches()){
            return false;
        }
        return true;
    }
    
    public static boolean isNumericOrNull(String str){
        if(null == str || str.equals("") || str.equals("null")){
            return true;
        }
        if(isInt(str))
            return true;
        Pattern pattern = Pattern.compile("^[0-9]{1,}\\.[0-9]{1,}$");
        Matcher isNum = pattern.matcher(str);
        if(!isNum.matches()){
            return false;
        }
        return true;
    }
    
    /**
     * 是否是整�?
     * @param str
     * @return
     */
    public static boolean isInt(String str){
        if(null == str||str.equals("")||str.equals("null")){
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if(!isNum.matches()){
            return false;
        }
        return true;
    }
    
    /**
     * 
     * @param d
     * @return
     */
    public static boolean checkDouble(Double d){
        if(null==d||d.compareTo(new Double(0))<=0){
            return false;
        }
        return true;
    }

    /**
     * 
     * @函数名称：getTwoDigtal
     * @功能描述：浮点数四舍五入保留两位小数
     * @param src
     * @return
     */
    public static float getTwoDigital(float src) {
        DecimalFormat decimalFormat = new DecimalFormat("00.00");
        return Float.parseFloat(decimalFormat.format(src));
        
    }
    public static float getTwoDigital(String src) {
        DecimalFormat decimalFormat = new DecimalFormat("00.00");
        return Float.parseFloat(decimalFormat.format(Float.parseFloat(src)));
    }
    public static Float getTwoDigitalFloat(String src){
        return new Float(getTwoDigital(src));
    }
    public static Float getTwoDigitalFloat(float src){
        return  new Float(getTwoDigital(src));
    }
    
}
